package com.xiaolin.dao.impl;

import com.xiaolin.bean.User;
import com.xiaolin.dao.BaseDAO;
import com.xiaolin.dao.UserDAO;

import java.sql.Connection;
import java.util.List;

public class UserDAOImpl extends BaseDAO<User> implements UserDAO {

    @Override
    public void insert(Connection conn, User user) {
        String sql = "INSERT INTO user(acNum,passWord,name,age,email,phone) VALUES(?,?,?,?,?,?)";
        update(conn, sql, user.getAcNum(), user.getPassWord(), user.getName(), user.getAge(), user.getEmail(),
                user.getPhone());
    }

    @Override
    public void deleteByAcNum(Connection conn, String acNum) {
        String sql = "DELETE FROM user WHERE acNum=?";
        update(conn,sql,acNum);
    }

    @Override
    public void update(Connection conn, User user) {
        String sql = "UPDATE user SET passWord = ? WHERE email = ?";
        update(conn, sql, user.getPassWord(), user.getEmail());
    }

    @Override
    public void updateByAcNum(Connection conn, User us) {
        String sql = "UPDATE user SET name=?,email=?,phone=?,age=? WHERE acNum=?";
        update(conn, sql, us.getName(),us.getEmail(),us.getPhone(),us.getAge(),us.getAcNum());
    }

    @Override
    public User getUserByEmail(Connection conn, String email) {
        String sql = "SELECT acNum,passWord,name,age,email,phone FROM user WHERE email = ?";
        User user = getInstance(conn, sql, email);
        return user;
    }

    @Override
    public User getUserByAcNum(Connection conn, String acNum) {
        String sql = "SELECT acNum,passWord,name,age,email,phone FROM user WHERE acNum=?";
        User user = getInstance(conn, sql, acNum);
        return user;
    }

    @Override
    public List<User> getAll(Connection conn) {
        String sql = "SELECT acNum,passWord,name,age,email,phone FROM user";
        List<User> list = getForList(conn, sql);
        return list;
    }
}